#include<stdio.h>
#include<Viewer_Interpreter/Viewer_OpenGL_Interpreter.h>
#include "include/Arcroll.h"
#include<Mesh_IO/Mesh_IO.h>
#include<tool/libcell_tools_view.h>
#include<tool/libcell_tools_formats.h>
#include<Math/Viewer_Matrix4x4.h>
#include<intersection_of_two_triangles.h>
#include<intersection_curve_of_two_surfaces.h>
#include<intersection_remesh_and_cut.h>


#define Matrix4x4 Viewer_Matrix4x4_

#include<macros.h>
#ifndef SIGN
#define SIGN(x) ((x>0)?1:((x<0)?-1:0))
#endif
#ifndef SAFE_SQRT
#define SAFE_SQRT(x) x>=0?sqrt(x):sqrt(-x)
#endif
#ifndef SAFE_FREE
#define SAFE_FREE(x) if(x!=NULL){free(x);}
#endif
#ifndef M_PI
#define M_PI 3.141592653589
#endif

typedef struct MY_INTER
{
    Viewer_Something* vs,* vs1;
    //Viewer_Faces* vf;
}MY_INTER;
void my_inter_key_test(Viewer_Intera* vi)
{
    Interactor_GlobalInfo*g_info=vi->g_info;
  //  MY_INTER* mi=(MY_INTER*)(vi->representation);
    if(g_info->key==VIEWER_KEY_D&&g_info->key_action==VIEWER_PRESS)
    {
    } 
    else if(g_info->key==VIEWER_KEY_B&&g_info->key_action==VIEWER_PRESS)
    {
    }
    else if(g_info->key==VIEWER_KEY_F&&g_info->key_action==VIEWER_PRESS)
    {
    }
    else if(g_info->key==VIEWER_KEY_N&&g_info->key_action==VIEWER_PRESS)
    {
    }
    else if(g_info->key==VIEWER_KEY_S&&g_info->key_action==VIEWER_PRESS)
    {
        if(g_info->pick_something!=NULL)
        {
            Viewer_Something* vs=(Viewer_Something*)(g_info->pick_something);
            if(strcmp(vs->name,"Faces")==0)
            {
                printf("pick id:%d\n",vs->marked_element );
            } 
        } 
    }

}
void show_line(Viewer_Edges* ve)
{
    double**V=(double**)malloc(sizeof(double*)*4);
    for(int i=0;i<4;i++)
    {
        V[i]=(double*)malloc(sizeof(double)*3);
        memset(V[i],0,sizeof(double)*3);
        V[i][0]=i*0.09;
        V[i][1]=i*i*0.09;
    }     
    int **C=(int**)malloc(sizeof(int*)*3);
    for(int i=0;i<3;i++)
    {
        C[i]=(int*)malloc(sizeof(int)*3);
        C[i][0]=2;
        for(int j=0;j<2;j++)
        {
            C[i][j+1]=i+j;
        }
    }
    Mesh  mesh;
    Mesh_init(&mesh);
    int num[3]={4,0,3};
    _ReadArray_(&mesh,V,NULL,C,num,3,1);

    get_data_from_1dim_cell(&mesh,&(ve->Data),&(ve->Data_index));


    Mesh_free(&mesh);  


    for(int i=0;i<4;i++)
    {
        free(V[i]);
    }
    free(V);
    for(int i=0;i<3;i++)
    {
        free(C[i]);
    }
    free(C);

}
void show_mesh(Viewer_Faces* vf,char* filename)
{
    Mesh mesh;
    Mesh_init(&mesh);
    //_ReadCell_(&mesh,"ti.cell"); 
    _ReadOff_(&mesh,filename,3);
    get_data_from_2dim_cell(&mesh,&(vf->Data),&(vf->Data_index));

    vf->Data_rows=mesh.num_v(&mesh);
    vf->Data_index_rows=mesh.num_c(&mesh);
    vf->color_rows=vf->Data_index_rows;
    vf->normal_rows=vf->Data_index_rows;
    double fac=0.00001;
    //double fac=1;
    for(int i=0;i<vf->Data_rows;i++)
    {
        vf->Data[i*3+0]*=fac;
        vf->Data[i*3+1]*=fac;
        vf->Data[i*3+2]*=fac;
    }

    Mesh_free(&mesh);

}
void load_points_data(Viewer_Points* vp,Node* n)
{
    vp->Data_rows=node_size(n)*2;
    vp->Data=(float*)malloc(sizeof(float)*3*vp->Data_rows);
    int i=0;
    for(Node* it=n;it!=NULL;it=(Node*)(it->Next))
    {
        double** p=(double**)(it->value);
         vp->Data[i*3+0]=p[0][0];vp->Data[i*3+1]=p[0][1];vp->Data[i*3+2]=p[0][2];
        i++;
        vp->Data[i*3+0]=p[1][0];vp->Data[i*3+1]=p[1][1];vp->Data[i*3+2]=p[1][2];
         i++;

    }
}
void show_intersection_points(Viewer_Points* vp)
{
    Mesh mesh1,mesh2,mesh;
    Mesh_init(&mesh1);Mesh_init(&mesh2);Mesh_init(&mesh);
    //mesh.
    _ReadOff_(&mesh1,"sphere.off",3);
    _ReadOff_(&mesh2,"sphere1.off",3);

    Node* n1=NULL,*n2=NULL,*temp_n;
    for(auto it=mesh1.c_begin(&mesh1);it!=mesh1.c_end(&mesh1);it++)
    {
        n1=node_overlying(n1,quote(it));
    } 
    for(auto it=mesh2.c_begin(&mesh2);it!=mesh2.c_end(&mesh2);it++)
    {
        n2=node_overlying(n2,quote(it));
    }

//simplify_node_of_two_nodes
    int flag=0,flag1=0;
    while(1)
    {
        temp_n=simplify_node_of_two_nodes(n1,n2,&flag1);
        if(flag1==0)
        {
            flag++;
        }
        free_node(n2);
        n2=n1;n1=temp_n;temp_n=NULL;

        if(flag>=2)
        {
            break;
        }
    }
    //Node* n3=get_intersection_lines_segments_of_two_nodes(n1,n2,&mesh);

    Node* n=NULL;
    for(Node* nit=n1;nit!=NULL;nit=(Node*)(nit->Next))
    {
        template_c* c1=(template_c*)(nit->value);
        for(Node* nit1=n2;nit1!=NULL;nit1=(Node*)(nit1->Next))
        {
            template_c* c2=(template_c*)(nit1->value);
            double**p=get_intersection_from_two_cells(c1,c2);
            if(p!=NULL)
            {
                n=node_overlying(n,p);
            }
        }

    }
   load_points_data(vp,n); 
   free_node_value(n);
   free_node(n);
    Mesh_free(&mesh1);   
    Mesh_free(&mesh2);
}

void show_intersection_edges(Viewer_Edges* ve)
{
    Mesh mesh1,mesh2,mesh;
    Mesh_init(&mesh1);Mesh_init(&mesh2);Mesh_init(&mesh);
    //mesh.
    _ReadOff_(&mesh1,"quad.off",3);
    _ReadOff_(&mesh2,"qinyang.off",3);
  
    get_intersection_lines_of_two_meshs(&mesh1,&mesh2,&mesh);

    ve->Data_index_rows=mesh.num_f(&mesh); 
    ve->Data_rows=ve->Data_index_rows*2;
    ve->Data=(float*)malloc(sizeof(float)*3*ve->Data_rows);
    ve->Data_index=(unsigned int*)malloc(sizeof(unsigned int)*2*ve->Data_index_rows);
    ve->color_rows=ve->Data_index_rows;
    int i=0;
    double fac=0.00001;
    //double fac=1;
    for(auto it=mesh.f_begin(&mesh);it!=mesh.f_end(&mesh);it++)
    {
        template_f*f=quote(it);
        template_v* v=f->vertices[0];
        ve->Data[(2*i+0)*3+0]=v->point[0]*fac;ve->Data[(2*i+0)*3+1]=v->point[1]*fac;ve->Data[(2*i+0)*3+2]=v->point[2]*fac;
        v=f->vertices[1];
        ve->Data[(2*i+1)*3+0]=v->point[0]*fac;ve->Data[(2*i+1)*3+1]=v->point[1]*fac;ve->Data[(2*i+1)*3+2]=v->point[2]*fac;
        ve->Data_index[i*2+0]=i*2+0;
        ve->Data_index[i*2+1]=i*2+1; 
        
        i++;
    }

//    interset_jiance(&mesh); 

    printf("%d\n",mesh.manifold_require);
    //Mesh mesh3;
    //Mesh_init(&mesh3);
    //Mesh_free(&mesh3);
    adjust_mesh_topology(&mesh); 
   // my_mesh_free(&mesh1);
    //Mesh_free(&mesh2);

    template_c* c=mesh2.get_cellp(&mesh2,94443);
  //  my_create_cell(c,&mesh2,&mesh,NULL);
    my_intersection_remesh(&mesh2,&mesh);


    Mesh_free(&mesh);

    Mesh_free(&mesh1);Mesh_free(&mesh2);

}
void show_mesh_lines(Viewer_Edges* ve,char* filename)
{
 double fac=0.00001;
    Mesh mesh;
    Mesh_init(&mesh);
    _ReadOff_(&mesh,filename,3); 
    ve->Data_rows=mesh.num_v(&mesh);
    ve->Data=(float*)malloc(sizeof(float)*3*ve->Data_rows);
    ve->Data_index_rows=mesh.num_f(&mesh);
    ve->Data_index=(unsigned int*)malloc(sizeof(unsigned int)*2*ve->Data_index_rows);
    ve->color_rows=ve->Data_index_rows;
    int i=0; 

    for(auto iter=mesh.v_begin(&mesh);iter!=mesh.v_end(&mesh);iter++)
    {
        ve->Data[i*3+0]=quote(iter)->point[0]*fac;
        ve->Data[i*3+1]=quote(iter)->point[1]*fac;
        ve->Data[i*3+2]=quote(iter)->point[2]*fac;
        i++;
    } 
    i=0;
    for(auto iter=mesh.f_begin(&mesh);iter!=mesh.f_end(&mesh);iter++)
    {
        ve->Data_index[i*2+0]=quote(iter)->vertices[0]->id;
        ve->Data_index[i*2+1]=quote(iter)->vertices[1]->id;
        i++;
    }
}



static inline  double *get_cell_plance_equation(template_c* c)
{
    
   // return get_triangle_plane_equation(c->vertices[0]->point,c->vertices[1]->point,c->vertices[2]->point);
} 


void test_viewer()
{
    Viewer_World_Manager vwm;
    viewer_world_manager_init(&vwm);
    Viewer_World *vw=vwm.create_world(&vwm,NULL);
    char camera[]="Camera";
    Node* n=vw->create_something(vw,camera);
    Viewer_Something* vs=(Viewer_Something*)(n->value);
    Viewer_Camera*vc=(Viewer_Camera*)(vs->evolution);
    vc->is_using=1;
    Matrix4x4*p=Projection(M_PI/3.0f,
        (float)(vw->g_info->resolution[0])/(float)(vw->g_info->resolution[1]),0.5f,200.0f);
    p->copy_data(vc->Proj,p);
    free_node(n);

    char edges[]="Edges";
    n=vw->create_something(vw,edges);
    vs=(Viewer_Something*)(n->value);
    vs->disappear=1;
    Viewer_Edges*ve=(Viewer_Edges*)(vs->evolution);
 //   ve->edgesize=5.0;
    show_mesh_lines(ve,"sphere1.off"); 
    float color[4]={0.9,0.9,0.9,1.0};
    ve->set_color(ve,color);
   
    free_node(n); 

    n=vw->create_something(vw,edges);
    vs=(Viewer_Something*)(n->value);
    ve=(Viewer_Edges*)(vs->evolution);
    show_intersection_edges(ve);
    color[0]=0;color[1]=0.1;color[2]=0.1;
    ve->set_color(ve,color);
    free_node(n);



    char intera[]="Intera";
    n=vw->create_something(vw,intera);
    vs=(Viewer_Something*)(n->value);
    Viewer_Intera *vi=(Viewer_Intera*)(vs->evolution);
    Viewer_Arcroll* va=(Viewer_Arcroll*)malloc(sizeof(Viewer_Arcroll));
    viewer_arcroll_init(va);
    va->mc=vc;
    va->vw=vw;

    vi->representation=va;
    vi->cursor_position_callback=viewer_arcroll_cursor_position_callback;
    vi->scroll_callback=viewer_arcroll_scroll_callback;
    vi->mouse_button_callback=viewer_arcroll_mouse_button_callback;
    vi->key_callback=viewer_arcroll_key_callback;
    free_node(n);




    char faces[]="Faces";
    n=vw->create_something(vw,faces);
    vs=(Viewer_Something*)(n->value);
    //vs->disappear=1;
    Viewer_Faces* vf=(Viewer_Faces*)(vs->evolution);
   // vf->Data_rows=6;
    //vf->Data_index_rows=2;
    //vf->color_rows=vf->Data_index_rows;
    show_mesh(vf,"quad.off");
    color[0]=0.9;color[1]=0.9;color[2]=0.9;
    vf->set_color(vf,color);
   // vf->color[164405*4+0]=0.5;vf->color[164405*4+1]=0.0;vf->color[164405*4+2]=0.0;
    //vf->color[86007*4+0]=0.5;vf->color[86007*4+1]=0.0;vf->color[86007*4+2]=0.0;

    free_node(n);
    
    MY_INTER* mi=(MY_INTER*)malloc(sizeof(MY_INTER));
    // mi->vs=vs;
    
    n=vw->create_something(vw,intera);
    vs=(Viewer_Something*)(n->value);
    vi=(Viewer_Intera*)(vs->evolution);
    vi->representation=mi;
    vi->key_callback=my_inter_key_test;



    n=vw->create_something(vw,faces);
    vs=(Viewer_Something*)(n->value);
   // mi->vs1=vs;
   //c vs->disappear=1;
    vf=(Viewer_Faces*)(vs->evolution);
    show_mesh(vf,"qinyang.off");
    vf->set_color(vf,color);
    free_node(n);

    // char edges[]="Edges";
    // n=vw->create_something(vw,edges);
    // vs=(Viewer_Something*)(n->value);
    // vs->disappear=1;
    // ve=(Viewer_Edges*)(vs->evolution);
    // show_mesh_lines(ve,"sphere.off");
    // ve->set_color(ve,color);
    // free_node(n); 

    char points[]="Points";
    n=vw->create_something(vw,points);
    vs=(Viewer_Something*)(n->value);
    //vs->disappear=1;
    Viewer_Points*vp=(Viewer_Points*)(vs->evolution); 
   //  show_intersection_points(vp);
    vp->Data_rows=2;
    vp->Data=(float*)malloc(sizeof(float)*3*vp->Data_rows);
    double fac=0.00001;
    vp->Data[0]=118133.00000000*fac;vp->Data[1]=89347.99999987*fac;vp->Data[2]=3000*fac;
    //vp->Data[3]=24242*fac;vp->Data[4]=104414*fac;vp->Data[5]=2900*fac; 
    // vp->Data[6]=138115*fac;vp->Data[7]=216239*fac;vp->Data[8]=3200*fac; 
    // vp->Data[9]=113222*fac;vp->Data[10]=79963*fac;vp->Data[11]=3200*fac; 
    // vp->Data[12]=113600*fac;vp->Data[13]=80338*fac;vp->Data[14]=3200*fac; 
    vp->pointsize=15.0;
    color[0]=0.7;color[1]=0.8;color[2]=0.7;color[3]=1.0;
    vp->set_color(vp,color);
    free_node(n);



    Viewer_Opengl_Interpreter voi;
    viewer_opengl_interpreter_initn(&voi,&vwm);    
    voi.interpreter(&voi);
    viewer_opengl_interpreter_free(&voi);
    viewer_world_manager_free(&vwm);  

}


void test_find_intersection1()
{
    double a1[3]={0,0,0},a2[3]={1,0,0},a3[3]={0,1,0};
    double b1[3]={0.2,0.3,0},b2[3]={3,5,0},b3[3]={1.6,1.2,2};
    //move
    double move[3]={0.8,-0.3,0};
    for(int i=0;i<3;i++)
    {
        b1[i]=b1[i]+move[i];
        b2[i]=b2[i]+move[i];
        b3[i]=b3[i]+move[i];

    }

    Matrix4x4* m=(Matrix4x4*)malloc(sizeof(Matrix4x4));
    Matrix4x4_init_double(m);
    double * data=(double*)(m->data);
    data[0*4+0]=1/sqrt(6);data[0*4+1]=1/sqrt(3);data[0*4+2]=1/sqrt(2);
    data[1*4+0]=2/sqrt(6);data[1*4+1]=-1/sqrt(3);data[1*4+2]=0;
    data[2*4+0]=1/sqrt(6);data[2*4+1]=1/sqrt(3);data[2*4+2]=-1/sqrt(2); 
    double temp_da1[3]={0,0,0},temp_da2[3]={0,0,0},temp_da3[3]={0,0,0};
    double temp_db1[3]={0,0,0},temp_db2[3]={0,0,0},temp_db3[3]={0,0,0};
    for(int i=0;i<3;i++)
    {    
        for(int j=0;j<3;j++)
        {
            temp_da1[i]+=data[i*4+j]*a1[j];
            temp_da2[i]+=data[i*4+j]*a2[j];
            temp_da3[i]+=data[i*4+j]*a3[j]; 
            temp_db1[i]+=data[i*4+j]*b1[j];
            temp_db2[i]+=data[i*4+j]*b2[j];
            temp_db3[i]+=data[i*4+j]*b3[j]; 
        }
    }
    for(int i=0;i<3;i++)
    {
            a1[i]=temp_da1[i];
            a2[i]=temp_da2[i];
            a3[i]=temp_da3[i]; 
            b1[i]=temp_db1[i];
            b2[i]=temp_db2[i];
            b3[i]=temp_db3[i]; 
    } 

   
    Matrix4x4_free(m);

}

void test()
{
    double *poly1[3];
    double* poly2[3]; 
    double tri1[9]={24242.00000000,104365.00000000,2915.00000000,25000.00000000,104365.00000000,
        2900.00000000,24242.00000000,105116.00000000,2686.00000000} ;
    double tri2[9]={400000,-400000,2900,400000,400000,2900,-400000,400000,2900};
    poly1[0]=&tri1[0];poly1[1]=&tri1[3];poly1[2]=&tri1[6];
    poly2[0]=&tri2[0];poly2[1]=&tri2[3];poly2[2]=&tri2[6];


    double ** re=get_intersection_points_from_two_polygons(poly1,3,poly2,3); 
    if(re!=NULL)
    {
        printf("shi %lf %lf %lf %lf %lf %lf\n",re[0][0],re[0][1],re[0][2],re[1][0],re[1][1],re[1][2]);
    }
}




void test_zkhy()
{
    Mesh mesh1,mesh2,mesh;
    Mesh_init(&mesh1);Mesh_init(&mesh2);Mesh_init(&mesh);
    //mesh.
    _ReadOff_(&mesh1,"quad.off",3);
    _ReadOff_(&mesh2,"1.off",3);
  
    get_intersection_lines_of_two_meshs(&mesh1,&mesh2,&mesh);
    adjust_mesh_topology(&mesh); 
     //adjust_mesh_topology(&mesh);
    // printf("herer\n");
   // my_mesh_free(&mesh1);
    //Mesh_free(&mesh2);

    // template_c* c=mesh2.get_cellp(&mesh2,167861);
    // printf("c:%d %d %d\n",c->vertices[0]->id,c->vertices[1]->id,c->vertices[2]->id );
    // my_create_cell(c,&mesh2,&mesh,NULL);
    Mesh* nm=my_intersection_remesh(&mesh2,&mesh);
    //my_test_cut(nm,&mesh);

    printf("nm numf:%d mesh num f:%d\n",nm->num_f(nm),mesh.num_f(&mesh) );
    

    my_intersection_cut(nm,&mesh);
    printf("nm numf:%d mesh num f:%d\n",nm->num_f(nm),mesh.num_f(&mesh) );
    _WriteCell_(nm,"newmesh.cell"); 
    // _WriteCell_(&mesh,"my.cell");

    Mesh_free(&mesh);

    Mesh_free(&mesh1);Mesh_free(&mesh2);
    //Mesh_free(nm);
    //free(nm);
}
void test_show_mesh(Viewer_Faces* vf,Mesh* nm)
{
  
 
    get_data_from_2dim_cell(nm,&(vf->Data),&(vf->Data_index));

    vf->Data_rows=nm->num_v(nm);
    vf->Data_index_rows=nm->num_c(nm);
    vf->color_rows=vf->Data_index_rows;
    vf->normal_rows=vf->Data_index_rows;
    double fac=0.00001;
    //double fac=1;
    for(int i=0;i<vf->Data_rows;i++)
    {
        vf->Data[i*3+0]*=fac;
        vf->Data[i*3+1]*=fac;
        vf->Data[i*3+2]*=fac;
    }
   
}

void load_mesh_points(Viewer_Points* vp,const char* filename)
{
    Mesh  mesh;
    Mesh_init(&mesh);
    _ReadOff_(&mesh,filename,3);
    printf("num v:%d\n",mesh.num_v(&mesh) );
        double fac=0.00001; 
    vp->Data_rows=mesh.num_v(&mesh);

    vp->Data=(float*)malloc(sizeof(float)*3*vp->Data_rows);
    int i=0;
    for(auto it=mesh.v_begin(&mesh);it!=mesh.v_end(&mesh);it++)
    {
        vp->Data[i*3+0]=quote(it)->point[0]*fac;
         vp->Data[i*3+1]=quote(it)->point[1]*fac;
        vp->Data[i*3+2]=quote(it)->point[2]*fac;
        i++;
    }   
    Mesh_free(&mesh);

}
void test_viewer1()
{

    Mesh mesh1,mesh2,mesh;
    Mesh_init(&mesh1);Mesh_init(&mesh2);Mesh_init(&mesh);
    //mesh.
    _ReadOff_(&mesh1,"quad.off",3);
    _ReadOff_(&mesh2,"qinyang.off",3);
  
    get_intersection_lines_of_two_meshs(&mesh1,&mesh2,&mesh);
    adjust_mesh_topology(&mesh); 

    
    Mesh* nm=my_intersection_remesh(&mesh2,&mesh);

   // printf("end remesh\n");
    Viewer_World_Manager vwm;
    viewer_world_manager_init(&vwm);
    Viewer_World *vw=vwm.create_world(&vwm,NULL);
    char camera[]="Camera";
    Node* n=vw->create_something(vw,camera);
    Viewer_Something* vs=(Viewer_Something*)(n->value);
    Viewer_Camera*vc=(Viewer_Camera*)(vs->evolution);
    vc->is_using=1;
    Matrix4x4*p=Projection(M_PI/3.0f,
        (float)(vw->g_info->resolution[0])/(float)(vw->g_info->resolution[1]),0.5f,200.0f);
    p->copy_data(vc->Proj,p);
    free_node(n); 

    // char faces[]="Faces";
    // n=vw->create_something(vw,faces);
    // vs=(Viewer_Something*)(n->value);
    // //vs->disappear=1;
    // Viewer_Faces* vf=(Viewer_Faces*)(vs->evolution);
    // test_show_mesh(vf,nm); 
    float color[4]={0.4,0.5,0.6,1.0};
    // vf->set_color(vf,color);
    // free_node(n);

    char intera[]="Intera";
    n=vw->create_something(vw,intera);
    vs=(Viewer_Something*)(n->value);
    Viewer_Intera *vi=(Viewer_Intera*)(vs->evolution);
    Viewer_Arcroll* va=(Viewer_Arcroll*)malloc(sizeof(Viewer_Arcroll));
    viewer_arcroll_init(va);
    va->mc=vc;
    va->vw=vw;

    vi->representation=va;
    vi->cursor_position_callback=viewer_arcroll_cursor_position_callback;
    vi->scroll_callback=viewer_arcroll_scroll_callback;
    vi->mouse_button_callback=viewer_arcroll_mouse_button_callback;
    vi->key_callback=viewer_arcroll_key_callback;
    free_node(n);


 //    char edges[]="Edges";
 //    n=vw->create_something(vw,edges);
 //    vs=(Viewer_Something*)(n->value);
 //    //vs->disappear=1;
 //    Viewer_Edges*ve=(Viewer_Edges*)(vs->evolution);
 // //   ve->edgesize=5.0;
 //    //show_mesh_lines(ve,"sphere1.off"); 
 //    test_show_edges(ve,&mesh,nm);
 //    //float color[4]={0.9,0.9,0.9,1.0};
 //    color[2]=0.9;color[1]=0.9;color[0]=0.9;
 //    ve->set_color(ve,color);
   
 //    free_node(n); 

    // n=vw->create_something(vw,edges);
    // vs=(Viewer_Something*)(n->value);
    // ve=(Viewer_Edges*)(vs->evolution);
    // show_mesh_lines(ve,"newmesh.off");
    // color[0]=0;
    // ve->set_color(ve,color);
    // free_node(n); 

    char points[]="Points";
    n=vw->create_something(vw,points);
    vs=(Viewer_Something*)(n->value);
    //vs->disappear=1;
    Viewer_Points*vp=(Viewer_Points*)(vs->evolution);
    load_mesh_points(vp,"newmesh.off");

    vp->pointsize=10;
    color[0]=0.1;color[1]=0.2;
    vp->set_color(vp,color);
    free_node(n);

    double fac=0.00001;  
    n=vw->create_something(vw,points);
    vs=(Viewer_Something*)(n->value);
    vs->disappear=1;
    vp=(Viewer_Points*)(vs->evolution);
    vp->Data_rows=1;
    vp->Data=(float*)malloc(sizeof(float)*3*vp->Data_rows);
    vp->Data[0]=118132.99999999804*fac;vp->Data[1]= 89347.99999984671*fac;vp->Data[2]= 3000.00000000000*fac;
    vp->pointsize=10;
    free_node(n);

    Mesh_free(&mesh);

    Mesh_free(&mesh1);Mesh_free(&mesh2); 

    Viewer_Opengl_Interpreter voi;
    viewer_opengl_interpreter_initn(&voi,&vwm);    
    voi.interpreter(&voi);
    viewer_opengl_interpreter_free(&voi);
    viewer_world_manager_free(&vwm);   
}
int main()
{
    //test_zkhy1();
    test_zkhy();
    //test_viewer1();
   //test_find_intersection1();
   // test_find_intersection();
     
     //objtooff("qinyang.obj");
  //  test_viewer();
  //  test();
    printf("end\n"); 
    return 0;
}
